package com.lean.pulltorefreshlistview.demo.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Toast;

import com.lean.pulltorefresh.list.base.PullToRefreshView;
import com.lean.pulltorefreshlistview.demo.R;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created by Lean·He on 16/7/28.
 */
public class MainActivity extends Activity {

    private PullToRefreshView listView;
    private ArrayAdapter<String> arrayAdapter;
    private Handler handler = new Handler();
    private Runnable refreshSuccessRunnable = new Runnable() {
        @Override
        public void run() {
            arrayAdapter.clear();
            arrayAdapter.addAll(generateDate());
            listView.onRefreshComplete();
        }
    };
    private Runnable onLoadMoreSuccessRunnable = new Runnable() {
        @Override
        public void run() {
            arrayAdapter.addAll(generateDate());
            listView.onLoadMoreComplete();
        }
    };
    private PullToRefreshView.OnRefreshListener onRefreshListener = new PullToRefreshView.OnRefreshListener() {
        @Override
        public void onRefresh() {
            handler.postDelayed(refreshSuccessRunnable, 3500);
        }
    };
    private PullToRefreshView.OnLoadMoreListener onLoadMoreListener = new PullToRefreshView.OnLoadMoreListener() {
        @Override
        public void onLoadMore() {
            handler.postDelayed(onLoadMoreSuccessRunnable, 3500);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toast.makeText(MainActivity.this, "SECOND " + Calendar.getInstance().get(Calendar.SECOND), Toast.LENGTH_SHORT).show();
        initListView();
    }

    private void initListView() {
        listView = (PullToRefreshView) findViewById(R.id.list_view);
        arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, generateDate());
        listView.setAdapter(arrayAdapter);
        listView.setonRefreshListener(onRefreshListener);
        listView.setOnLoadMoreListener(onLoadMoreListener);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.i("onItemClick", "onItemClick position " + position);
                Toast.makeText(MainActivity.this, String.valueOf(position), Toast.LENGTH_SHORT).show();
            }
        });
    }

    private List<String> generateDate() {
        List<String> listItems = new ArrayList<String>();
        for (int i = 0; i < 20; i++) {
            listItems.add("第 " + i + " 个 : "
                    + Calendar.getInstance().get(Calendar.MINUTE) + " : "
                    + Calendar.getInstance().get(Calendar.SECOND));
        }
        return listItems;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacks(refreshSuccessRunnable);
        handler.removeCallbacks(onLoadMoreSuccessRunnable);
    }
}
